/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dp1.titandevelop.titano.view.prod;

import dp1.titandevelop.titano.bean.Mensajes;
import dp1.titandevelop.titano.persistent.Almacen;
import dp1.titandevelop.titano.persistent.Empleado;
import dp1.titandevelop.titano.persistent.Proceso;
import dp1.titandevelop.titano.service.AlmacenService;
import dp1.titandevelop.titano.service.ConexionService;
import dp1.titandevelop.titano.service.ProcesoService;
import dp1.titandevelop.titano.service.ValidacionService;
import java.awt.Cursor;
import java.awt.Frame;
import java.io.File;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Yulian
 */
public class ModulosT_PRO_ReporteProduccion extends javax.swing.JInternalFrame {

    private List<Proceso> listaProc;
    
    private Mensajes sms = new Mensajes();
    public Empleado operario;
    
     public void llenaOperario (){
        
        this.txtOperario.setText(operario.getDni());
    }
     private void addList (){
        
        for(int i=0;i<listaProc.size();i++){
            this.cmbProceso.addItem(listaProc.get(i).getDescripcion());
        }
    }
    public ModulosT_PRO_ReporteProduccion() {
        initComponents();
        ValidacionService val = new ValidacionService();
        val.NoEscribe(dpFechaFin);
        val.NoEscribe(dpFechaIn);
        this.txtOperario.setEnabled(false);
        ProcesoService servProc = new ProcesoService();
        this.listaProc = new ArrayList<Proceso>();
        this.listaProc.addAll(servProc.buscarProcAll());
        addList();
    }
    public int cboxIndex(JComboBox cbox){
        return cbox.getSelectedIndex()-1;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnReporte = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cmbProceso = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        dpFechaIn = new org.jdesktop.swingx.JXDatePicker();
        jLabel7 = new javax.swing.JLabel();
        dpFechaFin = new org.jdesktop.swingx.JXDatePicker();
        jLabel2 = new javax.swing.JLabel();
        txtOperario = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setResizable(true);
        setTitle("Reporte de Produccion");

        btnReporte.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/reporte2.png"))); // NOI18N
        btnReporte.setToolTipText("Click para generar reporte");
        btnReporte.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReporteActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel1.setText("Proceso:");

        cmbProceso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar" }));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Rango"));

        jLabel6.setText("Fecha Mín:");

        jLabel7.setText("Fecha Máx:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(dpFechaIn, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dpFechaFin, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                .addGap(4, 4, 4))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(dpFechaIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(dpFechaFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setText("Operario:");

        txtOperario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtOperarioActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar2.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnReporte)
                                .addGap(0, 0, 0)
                                .addComponent(btnCancelar))
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cmbProceso, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtOperario, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)
                        .addGap(4, 4, 4)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(cmbProceso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel2)
                        .addComponent(txtOperario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1))
                .addGap(11, 11, 11)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnReporte, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnReporteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReporteActionPerformed
        
       ValidacionService val = new ValidacionService();
        
       if(!val.esFechaVacia(dpFechaFin)&&!val.esFechaVacia(dpFechaIn))
       {
            
            ConexionService serv = new ConexionService();
            Connection con =serv.getConexion();
            try{
                //Preparando el parámetros
                this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                int indexProceso =cboxIndex(cmbProceso), idProc;
                Date in= this.dpFechaIn.getDate(), fin = dpFechaFin.getDate();
                
                
                Map parametro = new HashMap();
                String proc = "%";
                String dni = "%";
                                
                if(!val.esCBoxVacio(cmbProceso))
                    proc = this.listaProc.get(indexProceso).getDescripcion();
                if(!val.esVacio(txtOperario))
                    dni = txtOperario.getText();
               
                    
                InputStream archivoReporte;
                archivoReporte =getClass().getResourceAsStream("/Reportes/Reporte_Productividad_Rotura.jasper");
                    parametro.put("proceso",proc);
                    parametro.put("dnioperario",dni);
                    parametro.put("fechaMin", in);
                    parametro.put("fechaMax", fin);
                    
                JasperReport jreport = (JasperReport) JRLoader.loadObject(archivoReporte);                
                JasperPrint jasperPrint = JasperFillManager.fillReport(jreport,parametro,con);
                JasperViewer jviewer = new JasperViewer(jasperPrint,false);  
                jviewer.setVisible(true); 
                this.setCursor(Cursor.getDefaultCursor());
            }catch(Exception e){
                System.out.println("mensaje de Error:"+ e.getMessage());
                JOptionPane.showInternalMessageDialog(this.getRootPane(),"Hubo un error en la generación del reporte","Error",JOptionPane.ERROR_MESSAGE);
            }
        }
       else{
            JOptionPane.showInternalMessageDialog(this.getRootPane(), "Debe ingresar el período ","Aviso",JOptionPane.WARNING_MESSAGE);
       }
       
                this.setCursor(Cursor.getDefaultCursor());
    
    }//GEN-LAST:event_btnReporteActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        ModulosT_PRO.repProd= null;
        this.dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
         // Busca un producto y lo carga en el txt -- Se va
        Frame f = JOptionPane.getFrameForComponent(this);
        ModulosT_PRO_BuscarOperarioRepor dialog = new ModulosT_PRO_BuscarOperarioRepor(f,true);
        dialog.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtOperarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtOperarioActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtOperarioActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnReporte;
    private javax.swing.JComboBox cmbProceso;
    private org.jdesktop.swingx.JXDatePicker dpFechaFin;
    private org.jdesktop.swingx.JXDatePicker dpFechaIn;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtOperario;
    // End of variables declaration//GEN-END:variables
}
